package com.lxl.mybatisplus.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxl.mybatisplus.entity.Book;
import com.lxl.mybatisplus.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;

    @Override
    public List<Book> findAllBooks() {
        return bookMapper.selectList(null);
    }

    @Override
    public Book findBookById(int id) {
        return bookMapper.selectById(id);
    }

    @Override
    public void addBook(Book book) {
        bookMapper.insert(book);
    }

    @Override
    public void updateBook(Book book) {
        bookMapper.updateById(book);
    }

    @Override
    public void deleteBook(int id) {
        bookMapper.deleteById(id);
    }

    @Override
    public IPage<Book> getPage(int pageNum,int size){
        IPage<Book> bookPage = new Page<>(pageNum, size);//参数一是当前页，参数二是每页个数
        bookPage = bookMapper.selectPage(bookPage, null);
        return bookPage;
    }

    @Override
    public List<Book> searchBooks(Book book) {
//        QueryWrapper<Book> queryWrapper=new QueryWrapper<>();
//        queryWrapper.like(book.getName()!=""&&book.getName()!=null,"name",book.getName());
//        queryWrapper.eq(book.getCategory()!=""&&book.getCategory()!=null,"category",book.getCategory());
//        queryWrapper.eq(book.getAuthor()!=""&&book.getAuthor()!=null,"author",book.getAuthor());
//        return bookMapper.selectList(queryWrapper);
        LambdaQueryWrapper<Book> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(book.getName()!=""&&book.getName()!=null,Book::getName,book.getName());
        queryWrapper.eq(book.getCategory()!=""&&book.getCategory()!=null,Book::getCategory,book.getCategory());
        queryWrapper.eq(book.getAuthor()!=""&&book.getAuthor()!=null,Book::getAuthor,book.getAuthor());
        return bookMapper.selectList(queryWrapper);
    }
}
